package org.geeks.demo;

import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import net.coobird.thumbnailator.ThumbnailParameter;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Position;
import net.coobird.thumbnailator.name.Rename;

/**
 * Generate image by country code
 * 
 * @author geek-s
 *
 */
public class WaterMarkShowcase {

	/**
	 * Main method
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		List<String> codes = Lists.newArrayList("cn", "us");

		WaterMarkShowcase waterMarkShowcase = new WaterMarkShowcase();

		Long start = System.currentTimeMillis();
		waterMarkShowcase.generateImg(codes);
		System.out.println("Mission complete in " + (System.currentTimeMillis() - start) + " ms");
	}

	PositionX us = newPosition("us", 180, 210);

	PositionX cn = newPosition("cn", 725, 222);

	PositionX ru = newPosition("ru", 680, 130);

	List<PositionX> positionXs = Lists.newArrayList(us, cn, ru);

	Map<String, PositionX> positionXMap = Maps.newHashMap();

	public WaterMarkShowcase() {
		positionXs.forEach((positionX) -> {
			positionXMap.put(positionX.getCode(), positionX);
		});
	}

	/**
	 * Generate image by code
	 * 
	 * @param codes
	 *            Country codes
	 * @throws Exception
	 *             Exception
	 */
	public void generateImg(List<String> codes) throws Exception {
		List<PositionX> positionXs = Lists.newArrayList();

		StringBuilder codeString = new StringBuilder();
		Collections.sort(codes);
		codes.forEach((code) -> {
			codeString.append(code);
			positionXs.add(positionXMap.get(code));
		});

		Builder<File> builder = Thumbnails.of("bg.png").size(970, 499);

		positionXs.forEach((positionX) -> {
			try {
				builder.watermark(positionX.getPosition(), ImageIO.read(new File("tag.png")), 1.0f);
			} catch (IOException e) {
				e.printStackTrace();
			}
		});
		builder.outputFormat("png").toFiles(new Rename() {

			@Override
			public String apply(String name, ThumbnailParameter param) {
				return Base64.getEncoder().encodeToString(codeString.toString().getBytes());
			}
		});
	}

	@AllArgsConstructor
	@Setter
	@Getter
	private class PositionX {

		/**
		 * Country code
		 */
		private String code;

		/**
		 * Position
		 */
		private Position position;
	}

	/**
	 * New Position
	 * 
	 * @param x
	 *            X
	 * @param y
	 *            Y
	 * @return Position Position
	 */
	public PositionX newPosition(String code, final int x, final int y) {
		return new PositionX(code, new Position() {

			public Point calculate(int enclosingWidth, int enclosingHeight, int width, int height, int insetLeft,
					int insetRight, int insetTop, int insetBottom) {
				return new Point(x, y);
			}
		});
	}
}
